﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data.Entity;
using System.Data.Linq;
using System.Data.Objects;
using kysec.RAL.EFToMySql;
using MySql.Data.MySqlClient;

namespace kysec.Bll.User
{
    public class Login
    {
        /// <summary>
        /// 用户登录的判断，返回json格式的数据
        /// </summary>
        /// <param name="model">参数集</param>
        /// <returns>0：用户不存在，1：密码错误，2：权限不对，3：用户在线状态，4：系统维护，5：登录成功</returns>
        public static bool LoginIsOk(kysec.RAL.EFToMySql.tb_user model)
        {
            MySql_kysecContectionString ctx = new MySql_kysecContectionString();

            string pwd = kysec.CR.Common.EncodeByBase64.Encode(model.c_vc_passWord);
            try
            {
                var list = ctx.tb_user.Where(c => c.c_vc_userName == model.c_vc_userName && c.c_vc_passWord == pwd && c.c_i_pop == model.c_i_pop && c.c_b_isDelete == false ).ToList();
                    if (list.Count != 0)
                    { 
                        foreach (var item in list)
                        {
                            item.c_b_isOnline = true;
                            bool ok = ctx.SaveChanges() == 1 ? true : false;
                            if (ok)
                            {
                                HttpContext.Current.Session["userId"] = kysec.CR.Common.EncodeByBase64.Encode(item.c_uq_userId.ToString());//现在不知道使用多线程是否真的能实现这个功能，不同用户同时在线时取得session中的值
                                HttpContext.Current.Session["pop"] = model.c_i_pop;
                                return true;
                            }
                        }                    
                        return false;
                    }
            }
            catch
            {
                kysec.CR.Logger.MyLogger.Error("系统维护中……");
                return false;
            }
            finally
            {
                ctx.Dispose();
            }

            return false;
        }

        public static void AddFirstUser()
        {
            MySql_kysecContectionString ctx = new MySql_kysecContectionString();
            string result = "0";
            Guid uId = Guid.NewGuid();           

            try
            {
                var list = ctx.tb_user.Select(c => c.c_vc_userName.Equals("wanghongwen") && c.c_b_isDelete == false).ToList();
                if (list.Count != 0)
                {
                    tb_user user = new tb_user
                    {
                        c_uq_userId = uId.ToString(),
                        c_vc_userName = "wanghongwen",
                        c_vc_passWord = kysec.CR.Common.EncodeByBase64.Encode("whw19881028"),
                        c_dt_regDate = DateTime.Now,
                        c_b_isComplete = false,
                        c_b_isDelete = false,
                        c_b_isOnline = false,
                        c_i_pop = 0

                    };
                    ctx.AddTotb_user(user);

                    result = ctx.SaveChanges().ToString();
                }
            }
            catch
            {
                kysec.CR.Logger.MyLogger.Error("插入用户数据发生异常……");
            }
            finally
            {
                ctx.Dispose();
            }            
        }
        /// <summary>
        /// 退出系统时，设置其OnLine状态为false
        /// </summary>
        public static void ExitSystem(string username)
        {
            MySql_kysecContectionString ctx = new MySql_kysecContectionString();
            try
            {
                string userName = kysec.CR.Common.EncodeByBase64.Decode(username);
                var list = ctx.tb_user.Where(c => c.c_vc_userName == userName).ToList();

                if (list.Count != 0)
                {
                    foreach (var item in list)
                    {
                        item.c_b_isOnline = false;
                    }
                   string result = ctx.SaveChanges().ToString();
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
                kysec.CR.Logger.MyLogger.Error("更改当前用户的状态出现异常！");
            }
            finally
            {
                ctx.Dispose();
            }

        }
    }
}
